package com.yy.micro.magic.data.rest;

import com.yy.micro.magic.data.domain.CodeItemVo;
import com.yy.micro.magic.data.domain.CodeMapVo;
import com.yy.micro.magic.data.resource.CodeMapResource;
import com.yy.micro.magic.data.service.code.MagicCodeItemService;
import com.yy.micro.magic.data.service.code.MagicCodeMapService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/magic-cube/data/code")
public class CodeRestful {

    @Autowired
    private MagicCodeMapService codeMapService;

    @Autowired
    private MagicCodeItemService codeItemService;

    @Autowired
    private CodeMapResource resource;

    @PostMapping("save")
    public CodeMapVo apply(@RequestBody CodeMapVo request){
        return codeMapService.apply(request);
    }

    @GetMapping("load/{id}")
    public CodeMapVo load(@PathVariable Long id){
        return codeMapService.load(id);
    }

    @GetMapping("find/{name}")
    public CodeMapVo find(@PathVariable String name){
        return codeMapService.findByName(name);
    }

    @PostMapping("item/save")
    public CodeItemVo applyItem(@RequestBody CodeItemVo request){
        return codeItemService.apply(request);
    }
    @GetMapping("item/load/{id}")
    public CodeItemVo loadItem(@PathVariable Long id){
        return codeItemService.load(id);
    }

    @GetMapping("item/find/{codeId}")
    public List<CodeItemVo> findItems(@PathVariable Long codeId){
        return codeItemService.findByCodeId(codeId);
    }

    @GetMapping("item/findByName/{codeName}")
    public CodeMapVo findCodeMap(@PathVariable String codeName){
        return resource.load(codeName);
    }

}
